package org.joor.harmony.slice;

import ohos.agp.components.Button;
import ohos.agp.components.Component;
import org.joor.harmony.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import org.joor.harmony.toast.Toast;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Button androidShortToastBtn = (Button) findComponentById(ResourceTable.Id_android_short_toast_btn);
        Button androidLongToastBtn = (Button) findComponentById(ResourceTable.Id_android_long_toast_btn);
        androidShortToastBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                toast("我来自鸿蒙世界", false);
            }
        });
        androidLongToastBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                toast("遥远的鸿蒙世界", true);
            }
        });
    }

    /**
     * 反射调用安卓平台的Toast组件
     */
    private void toast(String msg, boolean isLong) {
        int duration = isLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
        Toast.makeText(msg, duration).show();
    }
}